cmake_minimum_required(VERSION 3.5)

project(HttpServer CXX C)

set(CMAKE_CXX_STANDARD 17)

#Determin target architecture
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/TargetArchitecture.cmake)
target_architecture(TARGET_ARCH)
message("####### Target Architecture is ${TARGET_ARCH}")

#configure lib for different architecture
execute_process(COMMAND sh ${PROJECT_SOURCE_DIR}/tools/config_lib.sh ${TARGET_ARCH})

set(VERSION_HEAD_FILE ${PROJECT_SOURCE_DIR}/src/version.h)
execute_process(COMMAND awk /DA_MAJOR_VERSION/ ${VERSION_HEAD_FILE} OUTPUT_VARIABLE VERSION_STR)
string(REGEX MATCH "[0-9]+"  MAJOR_VERSION ${VERSION_STR})
execute_process(COMMAND awk /DA_MINOR_VERSION/ ${VERSION_HEAD_FILE} OUTPUT_VARIABLE VERSION_STR)
string(REGEX MATCH "[0-9]+"  MINOR_VERSION ${VERSION_STR})
execute_process(COMMAND awk /DA_PATCH_VERSION/ ${VERSION_HEAD_FILE} OUTPUT_VARIABLE VERSION_STR)
string(REGEX MATCH "[0-9]+"  PATCH_VERSION ${VERSION_STR})

string(TIMESTAMP COMPILE_TIME %Y%m%d%H%M%S)
message(STATUS "TIMESTAMP: ${COMPILE_TIME}")

set(DA_TARGET_VERSION "v${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}_${COMPILE_TIME}")

set(LIB_DIR ${PROJECT_SOURCE_DIR}/lib/linux_lib)

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -rdynamic -g -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} ${CMAKE_C_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/src/common/)
include_directories(${PROJECT_SOURCE_DIR}/include/)
include_directories(${PROJECT_SOURCE_DIR}/include/mysql)
include_directories(${PROJECT_SOURCE_DIR}/src/sqldb/)

message(FATA_INFO, ${PROJECT_SOURCE_DIR}/include)

file(GLOB _Source ${PROJECT_SOURCE_DIR}/src/*.cpp
                  ${PROJECT_SOURCE_DIR}/src/*.c
                  ${PROJECT_SOURCE_DIR}/src/common/*.c
                  ${PROJECT_SOURCE_DIR}/src/common/*.cpp
                  ${PROJECT_SOURCE_DIR}/src/sqldb/*.c
                  ${PROJECT_SOURCE_DIR}/src/sqldb/*.cpp
                  )

link_directories(${LIB_DIR})
find_library(LIBRT rt)
message(FATA_INFO, ${LIBRT})

set(LIB pthread ssl crypto mysqlclient ${LIBRT})

set(BIN_DIR ${PROJECT_SOURCE_DIR}/src/bin)

set(EXECUTABLE_OUTPUT_PATH ${BIN_DIR})

add_executable(${PROJECT_NAME} ${_Source})
target_link_libraries(${PROJECT_NAME} ${LIB})

#set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${DA_TARGET_VERSION})
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,--disable-new-dtags,-rpath,../linux_lib:./linux_lib")

# remove related files while make clean
set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ADDITIONAL_MAKE_CLEAN_FILES_LIST})

set(install_DIR ${PROJECT_SOURCE_DIR}/install/${PROJECT_NAME})

#何时执行自定义指令
#PRE_BUILD | PRE_LINK | POST_BUILD
# 编译前copy文件
add_custom_command(
        TARGET ${PROJECT_NAME}  PRE_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${PROJECT_SOURCE_DIR}/src/httpserver.cfg
        ${BIN_DIR}/httpserver.cfg)

add_custom_command(
        TARGET ${PROJECT_NAME}  PRE_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${PROJECT_SOURCE_DIR}/src/httpserver.sql
        ${BIN_DIR}/httpserver.sql)

add_custom_command(
        TARGET ${PROJECT_NAME}  PRE_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${PROJECT_SOURCE_DIR}/www
        ${BIN_DIR}/www)


install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION ${install_DIR}
        )

install(DIRECTORY ${LIB_DIR} DESTINATION ${install_DIR}
        PATTERN "*.sh" EXCLUDE # 剔除文件
        )

install(FILES ${PROJECT_SOURCE_DIR}/src/httpserver.cfg ${PROJECT_SOURCE_DIR}/src/httpserver.sql
        DESTINATION "${install_DIR}"
        COMPONENT devel
       )

install(DIRECTORY ${PROJECT_SOURCE_DIR}/www  DESTINATION ${install_DIR})

install(CODE "execute_process(COMMAND sh ${PROJECT_SOURCE_DIR}/tools/target_rename.sh ${PROJECT_NAME} ${DA_TARGET_VERSION} ${install_DIR})")